package com.esl.coordinator.command;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.esl.coordinator.exception.STiTEQATMELIntegrationException;
import com.esl.coordinator.util.ESLCommandUtil;
import com.esl.coordinator.util.Hex;

public class SetESLDeviceSleepTimeCommand extends Command {
	static final Logger LOG = LoggerFactory
			.getLogger(SetESLDeviceSleepTimeCommand.class);

	public static final String SET_ESL_DEVICE_SLEEP_TIME_LEAD_CODE = "CC";
	public static final String SET_ESL_DEVICE_SLEEP_TIME_LENGTH = "08";
	public static final String SET_ESL_DEVICE_SLEEP_TIME_COMMAND_TYPE = "60";
	public static final String SET_ESL_DEVICE_SLEEP_TIME_PADDING = "00";
	public static final int SET_ESL_DEVICE_SLEEP_SLEEP_MS_UNIT = 1000 / 500;

	private static final int EXPECTED_RESPONSE_BYTES = 4;

	private String macAddress;
	private int rev;
	private int sleepTime;

	public SetESLDeviceSleepTimeCommand(String macAddress, int rev,
			int sleepTime, CommandReceiver commandClient) {
		super(commandClient, EXPECTED_RESPONSE_BYTES);
		this.macAddress = macAddress;
		this.rev = rev;
		this.sleepTime = sleepTime;
	}

	@Override
	public String[] buildCommand() throws STiTEQATMELIntegrationException {
		StringBuffer messageString = new StringBuffer();
		messageString.append(SET_ESL_DEVICE_SLEEP_TIME_LEAD_CODE);
		messageString.append(SET_ESL_DEVICE_SLEEP_TIME_LENGTH);
		messageString.append(SET_ESL_DEVICE_SLEEP_TIME_COMMAND_TYPE);

		String lowMacByte = ESLCommandUtil.getMacLowByte(getmacAddress());
		String highMacByte = ESLCommandUtil.getMacHighByte(getmacAddress());

		messageString.append(lowMacByte);
		messageString.append(highMacByte);

		messageString.append(Hex.toHexString(getRev()));

		String sleepingTimeHex = Hex.toHexStringWithPadding(getSleepTime()
				* SET_ESL_DEVICE_SLEEP_SLEEP_MS_UNIT, 4);
		String sleepTimeFirstHexByte = ESLCommandUtil.getHexByteByByteIndex(
				sleepingTimeHex, 0);
		String sleepTimeSecondHexByte = ESLCommandUtil.getHexByteByByteIndex(
				sleepingTimeHex, 1);
		String sleepTimeThirdHexByte = ESLCommandUtil.getHexByteByByteIndex(
				sleepingTimeHex, 2);
		String sleepTimeFourthHexByte = ESLCommandUtil.getHexByteByByteIndex(
				sleepingTimeHex, 3);
		messageString.append(sleepTimeFourthHexByte);
		messageString.append(sleepTimeThirdHexByte);
		messageString.append(sleepTimeSecondHexByte);
		messageString.append(sleepTimeFirstHexByte);

		return new String[] { messageString.toString() };
	}

	@Override
	public boolean isCommandSuccessful() {
		return true;
	}

	public String getmacAddress() {
		return macAddress;
	}

	public void setmacAddress(String macAddress) {
		this.macAddress = macAddress;
	}

	public int getRev() {
		return rev;
	}

	public void setRev(int rev) {
		this.rev = rev;
	}

	public int getSleepTime() {
		return sleepTime;
	}

	public void setSleepTime(int sleepTime) {
		this.sleepTime = sleepTime;
	}

	public static void main(String[] args) {
		Command testCmd;
		try {
			testCmd = STiTEQATMELCommandFactory.getInstance()
					.createSetESLDeviceSleepTimeCommand(
							"08-8E",
							0,
							20,
							SocketCommandReceiver.getInstance("192.168.0.2",
									8088));
			CommandInvoker.sendCommand(testCmd);
			testCmd.isCommandSuccessful();
		} catch (STiTEQATMELIntegrationException e) {
			LOG.error("Error in test.", e);
		} catch (IOException e) {
			LOG.error("Error in test.", e);
		}
	}
}
